
var ticket_id = $("#last_ticket_id").val();
$(document).ready(function() {
    init();
    bind_user();
    bind_category();
    get_child_category();
    collection();
    ckeditor();
    dropzone();
    remove_image();
});


function dropzone() {
    if($('.dropzone').length <= 0 )
        return false;
    Dropzone.autoDiscover = false;
    //console.log('aaaa');
    $("#dropzone-image").dropzone({ 
        //        url: baseUrl+"item/uploadImage/id/" ,
        autoProcessQueue:false,
        addRemoveLinks: true,
        maxFilesize: 10,
        parallelUploads: 10,
        init: function() {
            var submitButton = $("#submit-upload-image");
            var submitRemove = $("#submit-remove-image");
            var myDropzone = this; // closure

            submitButton.on("click", function() {
                myDropzone.processQueue(); // Tell Dropzone to process all queued files.
            });
            submitRemove.on("click", function() {
                myDropzone.removeAllFiles(true); // Tell Dropzone to process all queued files.
            });

            // You might want to show the submit button only when 
            // files are dropped here:
 
            this.on("success", function (file,response) {
                
                $('#dropzone-image .dz-remove').hide();
                var response = JSON.parse(response);
                //myDropzone.removeAllFiles(true);
                $('.list-product-image').append(response.html);
                $('.remove-image').off('click');
                remove_image();
               
            });

        }
    });
}
    
function ckeditor() {
    // var full_editor = CKEDITOR;
    if ($('#full-ckeditor').length > 0)
    {
        CKEDITOR.replace('full-ckeditor', {
            toolbar: [
                ['Styles', 'Format', 'Font', 'FontSize'],
                '/',
                ['Bold', 'Italic', 'Underline', 'StrikeThrough', '-', 'Undo', 'Redo', '-', 'Cut', 'Copy', 'Paste', 'PasteFromWord', 'Find', 'Replace', '-', 'Outdent', 'Indent', '-', 'Print'],
                '/',
                ['NumberedList', 'BulletedList', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
                ['Image', 'Table', '-', 'Link', 'Flash', 'Smiley', 'TextColor', 'BGColor', 'Source', '-', 'Maximize']
            ]
        });
    }
//    if ($('#mini-ckeditor').length > 0) {
//        CKEDITOR.replace('mini-ckeditor', {
//            toolbar: [
//                ['Styles', 'Format', 'Font', 'FontSize'],
//                '/',
//                ['Bold', 'Italic', 'Underline', 'StrikeThrough', '-', 'Undo', 'Redo', '-', 'Cut', 'Copy', 'Paste', 'PasteFromWord', 'Find', 'Replace', ],
//                '/',
//                ['NumberedList', 'BulletedList', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
//                ['Maximize']
//            ]
//        });
//    }
    if ($('.mini-editor').length > 0)
        $(function() {
            $('.mini-editor').each(function() {
                CKEDITOR.replace($(this).attr('id'), {
                    toolbar: [
                        ['Styles', 'Format', 'Font', 'FontSize'],
                        '/',
                        ['Bold', 'Italic', 'Underline', 'StrikeThrough', '-', 'Undo', 'Redo', '-', 'Cut', 'Copy', 'Paste', 'PasteFromWord', 'Find', 'Replace', ],
                        '/',
                        ['NumberedList', 'BulletedList', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
                        ['Maximize']
                    ]
                });

            });
        });
}
function collection() {
    $('.btn-collection').click(function() {
        var ele = $(this);
        var parent = ele.parent();
        var type = ele.attr('type');
        var item = ele.attr('item');
        var collection = $('#collection').val();
        var data = {
            item: item,
            collection: collection,
            type: type
        };
        var url = baseUrl + 'collection/additem'
        $.post(url, data, function(result) {
            ele.addClass('hide');
            if (type == 'add')
                $('.btn-collection[type="remove"]', parent).removeClass('hide');
            else
                $('.btn-collection[type="add"]', parent).removeClass('hide');
        }, 'json');
        return false;
    })
}
function get_child_category() {
    $('.category-parent').change(function() {
        var ele = $(this);
        var id = $('option:selected', ele).val();
        var data = {
            id: id
        }
        var url = baseUrl + 'category/getchild/id/' + id;
        $.post(url, data, function(result) {
            console.log(result.html)
            $('.category-child').html(result.html);
        }, 'json');
        return false;
    });
}

function remove_image(){
     $(".remove-image").click(function() {
        if (!confirm("Are you sure delete this item?"))
            return false;
        var ele = $(this);
        $.get(ele.attr('link'), "", function() {
            ele.parents("li").fadeOut('slow');
            return false;
        });
        return false;
    });
}

function init() {
    $("table .delete-row").click(function() {
        if (!confirm("Are you sure delete this item?"))
            return false;
//        var ele = $(this);
//        $.get(ele.attr('href'), "", function() {
//            ele.parents("tr").fadeOut('slow');
//        });
//        return false;
    });
    
      


}

function bind_user() {
    $("#users .ban").on('click', function() {
        if (!confirm("Bạn có chắc banned ngườ?i dùng này không?"))
            return false;
        var ele = $(this);
        var parent = ele.parents('tr');

        $.get(ele.attr('href'), "", function(data) {
            if (data.success)
            {
                $(".label-banned", parent).show();
                ele.addClass('hide');
                $(".unban", parent).removeClass('hide');
            }
        }, 'json');
        return false;
    });

    $("#users .unban").on('click', function() {
        if (!confirm("Bạn có chắc unban ngư�?i dùng này không?"))
            return false;
        var ele = $(this);
        var parent = ele.parents('tr');

        $.get(ele.attr('href'), "", function(data) {
            if (data.success)
            {
                $(".label-banned", parent).hide();
                ele.addClass('hide');
                $(".ban", parent).removeClass('hide');
            }
        }, 'json');
        return false;
    });
}

function bind_category() {
    $(".category-type").change(function() {
        var ele = $(this);
        window.location = baseUrl + "category/index/type/" + ele.val();
    });
}

function display_error(msg) {
    $(".alert-success").hide();
    $(".alert-error .msg").html(msg);
    $(".alert-error").fadeIn('slow');
}

function display_success(msg) {
    $(".alert-error").hide();
    $(".alert-success .msg").html(msg);
    $(".alert-success").fadeIn('slow');
}

function number_format(number, decimals, dec_point, thousands_sep) {
    // *     example 1: number_format(1234.56);
    // *     returns 1: '1,235'
    // *     example 2: number_format(1234.56, 2, ',', ' ');
    // *     returns 2: '1 234,56'
    // *     example 3: number_format(1234.5678, 2, '.', '');
    // *     returns 3: '1234.57'
    // *     example 4: number_format(67, 2, ',', '.');
    // *     returns 4: '67,00'
    // *     example 5: number_format(1000);
    // *     returns 5: '1,000'
    // *     example 6: number_format(67.311, 2);
    // *     returns 6: '67.31'
    // *     example 7: number_format(1000.55, 1);
    // *     returns 7: '1,000.6'
    // *     example 8: number_format(67000, 5, ',', '.');
    // *     returns 8: '67.000,00000'
    // *     example 9: number_format(0.9, 0);
    // *     returns 9: '1'
    // *    example 10: number_format('1.20', 2);
    // *    returns 10: '1.20'
    // *    example 11: number_format('1.20', 4);
    // *    returns 11: '1.2000'
    // *    example 12: number_format('1.2000', 3);
    // *    returns 12: '1.200'
    // *    example 13: number_format('1 000,50', 2, '.', ' ');
    // *    returns 13: '100 050.00'
    // Strip all characters but numerical ones.
    number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
    var n = !isFinite(+number) ? 0 : +number,
            prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
            sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
            dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
            s = '',
            toFixedFix = function(n, prec) {
        var k = Math.pow(10, prec);
        return '' + Math.round(n * k) / k;
    };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}
